const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

// 因为Bellman-ford算法会有很多无用的松弛，所以只把当前更改过值（松弛过）的节点进行下一步的松弛
// 同样有负权回路时，会无限循环
void async function () {
    line = await readline();
    let [n, edge] = line.split(' ').map(Number),
        graph = new Array(n + 1).fill(0).map(() => new Array()),
        inQueue = new Array(n + 1).fill(false),
        minDist = new Array(n + 1).fill(Number.MAX_VALUE),
        start = 1;

    minDist[start] = 0
    while (line = await readline()) {
        // 初始化邻接表
        let [left, right, val] = line.split(' ').map(Number);
        graph[left].push(new Edge(right, val))
    }

    let queue = [start];
    while (queue.length > 0) {
        let cur = queue.shift()
        // 出队列后置为false，后续可以再进来
        inQueue[cur] = false
        // 循环邻接边
        for (let edgeItem of graph[cur]) {
            // 开始松弛，如果子节点距离顶点的距离松弛变小，更新
            if (minDist[cur] + edgeItem.val < minDist[edgeItem.to]) {
                minDist[edgeItem.to] = minDist[cur] + edgeItem.val
                // 如果子节点不在当前队列中，加入队列，去更新孙节点。因为后面的节点和依赖前边的节点，所以进行有效遍历
                if (inQueue[edgeItem.to] === false) {
                    queue.push(edgeItem.to)
                    inQueue[edgeItem.to] = true
                }
            }
        }
    }
    console.log(minDist[n] === Number.MAX_VALUE ? "unconnected" : minDist[n])
}()

// 边实体类
class Edge {
    constructor(to, val) {
        this.to = to;
        this.val = val;
    }
}